﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JCNT.DAL;
using Pub;

namespace JCNT.NoteApp
{
    public partial class FNode_Add : Form
    {
        public CTreeII ctree = new CTreeII();
        public string ID = "";
        public string RootID = "";
        public string sLab = "";
        public int EDT_STATE;   //0:null; 1:add; 2:edit;
        public int i_Typ_Pre = 0;
        public int i_Typ = 0;

        public FNode_Add()
        {
            InitializeComponent();
        }

        public void showVal(string ID)
        {
            if (ID == "") return;
            this.ID = ID;
            Cwp_note wp_note = ctree.get_wp_note(this.ID);

            //ctree.findNode(tView1, ID);
            if (EDT_STATE == 2)
            {
                tboxTit.Text = wp_note.Tit;
                Program.fmain.fillRchText(this.ID);
            }
            else
            {
                tboxTit.Text = "";
                tboxCon.Text = "";
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int i_btn = 0;
            if (sender is ToolStripMenuItem)
                i_btn = Convert.ToInt16(((ToolStripMenuItem)sender).Tag);

            if (sender is ToolStripButton)
                i_btn = Convert.ToInt16(((ToolStripButton)sender).Tag);

            if (sender is Button)
                i_btn = Convert.ToInt16(((Button)sender).Tag);

            if (sender is NotifyIcon)
                i_btn = Convert.ToInt16(((NotifyIcon)sender).Tag);

            switch (i_btn)
            {
                case 1:     //OK
                    //ctree.wp_note.Lab = cboxLab.Text;
                    //ctree.wp_note.Lab = tView1.SelectedNode.Text;
                    if (sLab == "")
                    {
                        MessageBox.Show("请选择目录！");
                        return;
                    }
                    ctree.wp_note.Lab = sLab;
                    ctree.wp_note.Tit = tboxTit.Text;
                    ctree.wp_note.Con = tboxCon.Text;
                    ctree.wp_note.Oth = cboxOth.Text;
                    //ctree.wp_note.RootID = ((MyComboBoxItem)cboxLab.SelectedItem).Value;
                    //ctree.wp_note.RootID = tView1.SelectedNode.Tag.ToString();
                    ctree.wp_note.RootID = RootID;
                    ctree.wp_note.SetDat = CPub.sNow();

                    //Add
                    if (EDT_STATE == 1)
                    {
                        this.ID = ctree.newNodeID();
                        ctree.wp_note.ID = this.ID;
                        ctree.noteAdd(ctree.wp_note);
                    }
                    //Edit
                    else if (EDT_STATE == 2)
                    {
                        ctree.wp_note.ID = this.ID;
                        ctree.noteEdt(ctree.wp_note);
                        Program.ftree.tViewLab.SelectedNode.Text = tboxTit.Text;
                    }

                    ctree.conEdt(ID, tboxCon);
                    Program.ftree.ref_Tree();

                    btnOK_Click(btnCancel, null);

                    tboxCon.Text = "";
                    tboxTit.Clear();
                    break;

                case 2:     //cancel
                    tboxCon.Text = "";
                    tboxTit.Clear();
                    //Program.fmain.tboxCon.Visible = true;
                    Program.fmain.tabCtrl_Txt.Visible = true;
                    this.Hide();
                    break;
            }

        }

        private void FNode_Add_Load(object sender, EventArgs e)
        {
            tboxCon.Dock = DockStyle.Fill;
            tboxConII.Dock = DockStyle.Fill;
            webBrowCon.Dock = DockStyle.Fill;

            tboxConII.Visible = false;
            webBrowCon.Visible = false;
            CTextEditMenu richMenu = new CTextEditMenu(tboxCon);

        }



        private void FNode_Add_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void tboxTit_Enter(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tboxCon.Text)) return;
            if (string.IsNullOrEmpty(tboxTit.Text))
            {
                string[] s1 = tboxCon.Text.Substring(0, 200).Replace("\r", "").Split('\n');
                for (int i = 0; i < 5;i++ )
                {
                    if (!string.IsNullOrEmpty(s1[i]))
                    {
                        tboxTit.Text = s1[i];
                        break;
                    }
                }
                
            }
        }
    }
}
